﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
// add
using System.Diagnostics;
using System.IO;

namespace ConsoleBase
{
    public class CAction
    {
        string msSubject = string.Empty;
        StringBuilder msLog = new StringBuilder();
        DateTime dBegin = new DateTime();
        DateTime dEnd = new DateTime();
        bool mbResult;
        public int miStep { get; set; }
        public string msMsg { get; set; }
        public string Subject { get { return msSubject; } }
        public bool Result { get { return mbResult; } }
        public string msResult { get; set; }
        //bool mbDebug;
        public string Log { get { return msLog.ToString(); } }
        public DateTime BeginTime { get { return dBegin; } }
        public DateTime EndTime { get { return dEnd; } }
        public string msFilename { get; set; }
        public CAction()
        {
            dBegin = DateTime.Now;
            IsDebug = true;
        }
        public bool IsDebug { get; set; }  // 預設開啟, 預設讀取設定後關閉.

        public void LogClear()
        {
            msLog.Length = 0;
        }
        public void AppendMsg(string sMsg)
        {
            msMsg = sMsg;
            dEnd = DateTime.Now;
            msLog.AppendLine(string.Format("{0} {1}.", PrefixEndTime(), sMsg));
        }
        public void DebugMsg(string sMsg)
        {
            if (IsDebug)
            {
                msMsg = sMsg;
                dEnd = DateTime.Now;
                msLog.AppendLine(string.Format("{0} DEBUG {1}.", PrefixEndTime(), sMsg));
            }
        }
        public void Error(string sMsg)
        {
            Debug.WriteLine(sMsg);
            mbResult = false;
            msMsg = sMsg;
            dEnd = DateTime.Now;
            msLog.AppendLine(string.Format("{0} ERROR {1}.", PrefixEndTime(), sMsg));
        }
        public void Success(string sResult)
        {
            Debug.WriteLine(sResult);
            mbResult = true;
            msResult = sResult;
            dEnd = DateTime.Now;
            msLog.AppendLine(string.Format("{0} SUCCESS Result={1}.", PrefixEndTime(), sResult));
        }
        private string PrefixEndTime()
        {
            //msLog.AppendLine(string.Format("{0} Begin: {1}", dEnd.ToString("yyyyMMddHHmmssfff")));
            return dEnd.ToString("HH:mm:ss");
        }
        public void Begin(string sSubject)
        {
            Debug.WriteLine(sSubject);

            msSubject = sSubject;
            mbResult = false;
            msResult = string.Empty;
            miStep = 0;
            dBegin = DateTime.Now;
            dEnd = dBegin;

            LogClear();
            msLog.AppendLine(string.Format("{0} BEGIN Subject={1}, {2}.", PrefixEndTime(), sSubject, dEnd.ToString("yyyy-MM-dd HH:mm:ss.fff")));
        }
        public void SaveLog()
        {
            dEnd = DateTime.Now;
            if (string.IsNullOrEmpty(msFilename))
                msFilename = @".\Log-" + dEnd.Second.ToString() + ".txt";

            File.WriteAllText(msFilename, Log);
        }
    }
}
